﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using MyHealthByDesign.Helper;

namespace MyHealthByDesign
{
    public partial class Register : System.Web.UI.Page
    {
        ValidationError validationError = new ValidationError();
        Domain.Domain domain = new Domain.Domain();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Ethnicity_Checked(object sender, EventArgs e)
        {
            if(rdoOther.Checked)
            {
                txtOtherEthnicity.Enabled = true;
            }
            else
            {
                txtOtherEthnicity.Text = String.Empty;
                txtOtherEthnicity.Enabled = false;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try 
	        {	        
                string lastname = txtLastName.Text.Trim();
                string firstname = txtFirstname.Text.Trim();
                string birthDate = txtBirthdate.Text.Trim();
                string city = txtCity.Text.Trim();
                string zipCode = txtZipCode.Text;
                string email = txtEmail.Text;
                string password = txtPassword.Text;
                string mobile = !string.IsNullOrWhiteSpace(txtMobile.Text) ? txtMobile.Text : "NULL";
                string textFlag = chkBxText.Checked ? "Y" : "N";
                string sex = rdoMale.Checked ? "M" : "F";
                string ethnicity = GetEthnicity();

	            var textInfo = new CultureInfo("en-US", false).TextInfo;
	            lastname = textInfo.ToTitleCase(lastname.ToLower());
                firstname = textInfo.ToTitleCase(firstname.ToLower());
                city = textInfo.ToTitleCase(city.ToLower());

                //TODO: need to check for valid birthdate)))

                if (CheckUserInput(email, password, zipCode))
                {
                    if(domain.RegisterUser(lastname, firstname, birthDate, city, zipCode, email, password, mobile, textFlag,
                                        sex, ethnicity))
                    {
                        Response.Redirect("Views/MyHealth.aspx");
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
	        }
	        catch (Exception ex)
	        {
		        validationError.DisplayErrorMessage("Sorry! We couldn't register you due to problem in system. Please try again.");
	        }
        }

        private string GetEthnicity()
        {
            string ethnicity = string.Empty;

            if(rdoCaucasian.Checked)
            {
                ethnicity = "Caucasian (non-Hispanic)";
            }
            else if(rdoAfrican.Checked)
            {
                ethnicity = "African American (non-Hispanic)";
            }
            else if(rdoHispanic.Checked)
            {
                ethnicity = "Hispanic Latino";
            }
            else if(rdoEastern.Checked)
            {
                ethnicity = "Middle Eastern";
            }
            else if(rdoNative.Checked)
            {
                ethnicity = "Native American";
            }
            else if(rdoPacific.Checked)
            {
                ethnicity = "Pacific Islander";
            }
            else if(rdoAsian.Checked)
            {
                ethnicity = "Asian";
            }
            else if(rdoOther.Checked)
            {
                ethnicity = txtOtherEthnicity.Text.Trim();
            }

            return ethnicity;
        }

        
        private bool CheckUserInput(string email, string password, string zipCode)
        {
            bool validUserInput = true;

            if(!CheckEmail(email))
            {
                validationError.DisplayErrorMessage("Invalid Email Address.");
                validUserInput = false;
            }

            if(IsEmailRegistered(email))
            {
                validationError.DisplayErrorMessage("This email address is already registered in system.");
                validUserInput = false;
            }
           
            if(!CheckPasswordLength(password))
            {
                validationError.DisplayErrorMessage("Password should be minimum 8 characters.");
                validUserInput = false;
            }

            if (!CheckZipCode(zipCode))
            {
                validationError.DisplayErrorMessage("Invalid zipcode.");
                validUserInput = false;
            }

            return validUserInput;
        }

        private bool CheckEmail(string email)
        {
            bool validEmail = false;
            
            var regex = new Regex(Constants.EMAIL_PATTERN, RegexOptions.IgnoreCase);

            if(regex.IsMatch(email))
            {
                validEmail = true;
            }

            return validEmail;
        }

        private bool IsEmailRegistered(string email)
        {
            bool isRegistered = true;

            if(!domain.IsEmailRegistered(email))
            {
                isRegistered = false;   
            }

            return isRegistered;
        }

        private bool CheckPasswordLength(string password)
        {
            bool validPassword = false;
            if(password.Length >= Constants.PASSWORD_LENGTH)
            {
                validPassword = true;
            }

            return validPassword;
        }

        private bool CheckZipCode(string zipCode)
        {
            bool validZipCode = false;

            var regex = new Regex(Constants.ZIP_PATTERN, RegexOptions.IgnoreCase);

            if (regex.IsMatch(zipCode))
            {
                validZipCode = true;
            }

            return validZipCode;
        }
    }
}